package cn.aitrox.ry.service.anno.service.impl;

import cn.aitrox.ry.bean.RespResult;
import cn.aitrox.ry.common.service.BaseService;
import cn.aitrox.ry.enumtype.RespCodeEnum;
import cn.aitrox.ry.service.anno.dao.AnnoComponentDao;
import cn.aitrox.ry.service.anno.dao.AnnoLesionToolDao;
import cn.aitrox.ry.service.anno.dao.TaskDao;
import cn.aitrox.ry.service.anno.dto.anno.ComponentDto;
import cn.aitrox.ry.service.anno.dto.anno.LesionDto;
import cn.aitrox.ry.service.anno.dto.anno.setting.out.AnnoSettingInfoDto;
import cn.aitrox.ry.service.anno.entity.AnnoComponentEntity;
import cn.aitrox.ry.service.anno.entity.AnnoLesionToolEntity;
import cn.aitrox.ry.service.anno.entity.TaskEntity;
import cn.aitrox.ry.service.anno.service.AnnoSettingService;
import cn.aitrox.ry.service.config.enumtype.LesionToolBizTypeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AnnoSettingServiceImpl extends BaseService implements AnnoSettingService {

    @Autowired
    private AnnoComponentDao annoComponentDao;

    @Autowired
    private AnnoLesionToolDao annoLesionToolDao;

    @Autowired
    private TaskDao taskDao;

    @Override
    public RespResult<AnnoSettingInfoDto> read(Integer taskId) {
        TaskEntity task = taskDao.findById(taskId);
        if (null == task) {
            return RespResult.secByError(RespCodeEnum.NOT_EXIST.getCode(), "任务不存在");
        }
        List<AnnoLesionToolEntity> altList = annoLesionToolDao.findByTaskId(taskId);

        List<AnnoComponentEntity> acList = annoComponentDao.findByTaskId(taskId);

        List<ComponentDto> seriesCompList = acList.stream().filter(ac -> StringUtils.isEmpty(ac.getLesion())).map(ac -> {
            return new ComponentDto(ac.getComponentId(), ac.getRequired());
        }).collect(Collectors.toList());

        List<LesionDto> lesionList = new ArrayList<>();
        altList.stream().collect(Collectors.groupingBy(AnnoLesionToolEntity::getLesion)).forEach((lesionValue, altListByLesion) -> {
            List<String> toolList = altListByLesion.stream().map(AnnoLesionToolEntity::getTool).collect(Collectors.toList());
            List<ComponentDto> imageCompList = acList.stream().filter(ac -> lesionValue.equals(ac.getLesion())).map(ac -> {
                return new ComponentDto(ac.getComponentId(), ac.getRequired());
            }).collect(Collectors.toList());
            LesionDto lesion = new LesionDto(lesionValue, altListByLesion.get(0).getLesionColor(), toolList, imageCompList, altListByLesion.get(0).getBizType());
            lesionList.add(lesion);
        });

        AnnoSettingInfoDto info = new AnnoSettingInfoDto();
        info.setTaskName(task.getName());
        info.setSeriesCompList(seriesCompList);
        info.setLesionList(lesionList);
        info.setDiscardCodeList(task.getDiscardCode());
        info.setDefaultWindow(task.getDefaultWindow());
        info.setFindings(task.getFindings());
        info.setDiagnosis(task.getDiagnosis());
        info.setShowYay(task.getShowYay());
        info.setShowReport(task.getShowReport());
        info.setImageLayout(task.getImageLayout());
        info.setNeedVideoTrace(altList.stream().filter(x -> LesionToolBizTypeEnum._isAlgTrace(x.getBizType())).findFirst().orElse(null) != null);

        return RespResult.ok(info);
    }
}
